package view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import metier.ConfigFiltre;

public class FClasser extends JFrame {
	
	JLabel pathFiltre;
	JButton parcourir,create,voir,execute,save;
	ConfigFiltre config;
	String path;
	
	public FClasser() {
		super();
		init();
		setLayout(new GridLayout(4,1));
		add(TopPanel());
		voir = new JButton("Voir Filtre");
		voir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			// frame voir filtre
		}});
		
		execute = new JButton("Exécuter Filtre");
		execute.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// executer filtre
			}});
		save = new JButton("Enregistrer Filtre");
		save.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			    JFileChooser chooser = new JFileChooser();
			    chooser.setCurrentDirectory(new File("~/"));
			    int retrival = chooser.showSaveDialog(null);
			    if (retrival == JFileChooser.APPROVE_OPTION) {
			        try {
			            FileWriter fw = new FileWriter(chooser.getSelectedFile()+".txt");
			            fw.write(config.toString());
			            fw.close();
			        } catch (Exception ex) {
			            ex.printStackTrace();
			        }
			    }
			}});
		
		add(voir);
		add(execute);
		add(save);
		
        setVisible(true);
	}
	
	public void init() {
		setTitle("Fouille De Textes - 2015");
	    setSize(new Dimension(470, 400));
	    setLocationRelativeTo(null);
	    
	    config = new ConfigFiltre();
	}
	
	public JPanel TopPanel() {
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,2));
		pathFiltre = new JLabel("Filtre : ");
		res.add(pathFiltre);
		parcourir = new JButton("Import Filtre");
		res.add(parcourir);
		parcourir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser openFile = new JFileChooser();
                openFile.showOpenDialog(null);

                try {
                    // Open an input stream
                    Scanner reader = new Scanner(openFile.getSelectedFile());
                    if(reader != null) {
                        path = openFile.getSelectedFile().toString();
                        String[] tabs = path.split("/");
                        String res = tabs[tabs.length-1];
                        res = res.substring(0, res.length()-4);
                        pathFiltre.setText("Filtre : "+res);
                        MAJFiltre();
                    }
                }
                catch (FileNotFoundException x){
                    System.out.println("Error :"+x);
                }
                catch(NullPointerException x){
                    System.out.println("file null : "+x);
                }

            }
			
		});
		
		return res;
	}
	
	public void MAJFiltre() {;
		String[] args = new String[4];
		try{
			int index = 0;
			InputStream ips=new FileInputStream(path); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			while ((ligne=br.readLine())!=null){
				if(!ligne.contains("#")){
					String[] tab = ligne.split(":");
					args[index++]=(tab[1]);
				}
			}
			br.close(); 
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
		
		config.setLemmatisation(Boolean.valueOf(args[0].substring(1)));
		config.setAccents(Boolean.valueOf(args[1].substring(1)));
		config.setAdjPossessif(Boolean.valueOf(args[2].substring(1)));
		config.setSmallChar(Boolean.valueOf(args[3].substring(1)));
		
		System.out.println(config.toString());
		
	}

}
